/*
 * Copyright 2008 Karel Klic.
 * This file is part of Oslavanský Bot.
 * 
 * Oslavanský Bot is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package categorybot;

import java.util.ArrayList;
import java.util.regex.*;

/**
 * Spojeni mluvnickeho druhu a jeho kategorii.
 * @author Karel
 */
public class WordType {

  ArrayList<String> typeIdList;
  ArrayList<Pattern> typePatternList;
  CategoryList categoryList;

  /**
   * Standard constructor.
   * @param typeId
   * Linguistic type such as podstatné jméno.
   * @param categoryIdList
   * A list of categories without prefix (prefix "Kategorie:").
   */
  public WordType(ArrayList<String> typeIdList, CategoryList categoryList) {
    init(typeIdList, categoryList);
  }
  
  /**
   * Standard constructor.
   * @param typeId
   * Linguistic type such as podstatné jméno.
   * @param categoryIdList
   * A list of categories without prefix (prefix "Kategorie:").
   */
  public WordType(String typeId, CategoryList categoryList) {
    ArrayList<String> list = new ArrayList<String>();
    list.add(typeId);
    init(list, categoryList);
  }
  
  private void init(ArrayList<String> typeIdList, CategoryList categoryList) {
    this.typeIdList = typeIdList;
    this.categoryList = categoryList;
    
    typePatternList = new ArrayList<Pattern>();
    for (String typeId : typeIdList)
      typePatternList.add(PatternFactory.getHeader3Pattern(typeId));
  }
      
  /**
   * 
   * @param text
   * Kompletni text slovicka.
   * @param startLanguageArea
   * Zacatek naseho jazyka. Prvni znak.
   * @param endLanguageArea
   * Ukazatel za posledni znak naseho jazyka.
   * @return
   * True pokud byl nejaky problem se slovickem, tedy je potreba jej opravit.
   */
  public ArrayList<SolveCategoryResult> solveWord(String text, int startLanguageArea, int endLanguageArea) {
    ArrayList<SolveCategoryResult> result = new ArrayList<SolveCategoryResult>();
    
    boolean found = false;
    for (Pattern typePattern : typePatternList) {
      Matcher typeMatcher = typePattern.matcher(text);
      typeMatcher.region(startLanguageArea, endLanguageArea);
      
      if (typeMatcher.find()) {
        // Type found, check that the categories are presented.
        int startTypeArea = typeMatcher.end();
        int endTypeArea = endLanguageArea;
        Matcher nextTypeMatcher = PatternFactory.getHeader3Pattern().matcher(text);
        nextTypeMatcher.region(startTypeArea, endLanguageArea);
        if (nextTypeMatcher.find()) {
          endTypeArea = nextTypeMatcher.start();
        }

        // Type is used within the language area, so the category must be presented somewhere in
        // the text.
        result.addAll(categoryList.solveContainsCategory(text, startTypeArea, endTypeArea));
        found = true;
        break;
      }
    }
    
    if (!found) {
      // Type not found, check that the categories are NOT presented.
      result.addAll(categoryList.solveNotContainsCategory(text));
    }
    
    return result;
  }
}
